package com.zcf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcf.common.json.Body;
import com.zcf.mapper.GameteaMapper;
import com.zcf.pojo.Gametea;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;
import com.zcf.utils.PropertiesUtil;
import com.zcf.vo.out.LayuiJson;
@Service
public class GameteaServiceImpl {
	
	@Autowired
	GameteaMapper gm;
	/**
	 * 后台获取元帅茶苑房间列表
	 * @return
	 */
	public LayuiJson getRooms(RoomsParam param) {
		LayuiJson lj = new LayuiJson();
		Map<String, RoomsParam> rooms = null;
		Map<String, Object> roominfo ;
		List<Object> list = new ArrayList<>();
		for (RoomsParam room : rooms.values()) {
			roominfo = new HashMap<>();
			if(param.getRoomName() != null){
				if(room.getRoomName().equals( param.getRoomName())){
					list.clear();
					Integer[] personnum = new Integer[2];
					personnum[1] = room.getRoomType().get("personnum");
					personnum[0] = room.getUsers().size();
					roominfo.put("roomName", room.getRoomName());
					roominfo.put("room",room.getRoomType().get("room"));
					roominfo.put("small",room.getRoomType().get("small"));
					roominfo.put("tea",room.getRoomType().get("tea"));
					roominfo.put("personnum", personnum);
					roominfo.put("points", room.getRoomType().get("points")+1);
					roominfo.put("maxz", room.getRoomType().get("maxz"));
					roominfo.put("double", room.getRoomType().get("double"));
					roominfo.put("roomType", room.getRoomType().get("roomType"));
					list.add(roominfo);
					if(list.size() != 0){
						lj.setCode(0);
						//			lj.setCount(count);
						lj.setData(list);
						lj.setMsg("成功");
						return lj;
					}
				}
			}
			Integer[] personnum = new Integer[2];
			personnum[1] = room.getRoomType().get("personnum");
			personnum[0] = room.getUsers().size();
			roominfo.put("roomName", room.getRoomName());
			roominfo.put("room",room.getRoomType().get("room"));
			roominfo.put("small",room.getRoomType().get("small"));
			roominfo.put("tea",room.getRoomType().get("tea"));
			roominfo.put("personnum", personnum);
			roominfo.put("points", room.getRoomType().get("points")+1);
			roominfo.put("maxz", room.getRoomType().get("maxz"));
			roominfo.put("double", room.getRoomType().get("double"));
			roominfo.put("roomType", room.getRoomType().get("roomType"));
			list.add(roominfo);
		}
		if(list.size() != 0){
			lj.setCode(0);
			//			lj.setCount(count);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		
		lj.setCode(1);
		//		lj.setCount(count);
		lj.setData(list);
		lj.setMsg("暂无数据");
		return lj;
	}
	/**
	 * 添加元帅茶苑
	 * @param gametea 
	 * @return
	 */
	public Body addgame(Gametea gametea) {
//		WebSocketBull ws = new WebSocketBull();
//		Map<String, RoomsParam> rooms = null;
//		Map<String, Integer> roomtype = new HashMap<>();
//		roomtype.put("personnum", gametea.getPersonnum());//人数
//		roomtype.put("room", gametea.getRoom());//
//		roomtype.put("points", gametea.getPoints());//底分
//		roomtype.put("double", gametea.getDoubles());
//		roomtype.put("num",3);
//		roomtype.put("opentable", 2);//自动开桌
//		roomtype.put("maxz", gametea.getMaxz());//最大抢庄
//		roomtype.put("roomType", 1); //房间类型
//		String[] strs = null,sss = null;
//		String str = gametea.getHoptions(); //高级选项
//		if(str.length()!=0){
//			strs =  str.split(",");
//		}
//		String ss = gametea.getSpecialcard(); //高级选项
//		if(ss.length()!=0){
//			sss =  ss.split(",");
//		}
//		//创建房间
//		RoomsParam room = new RoomsParam();
//		Random random = new Random();
//		String roomName = String.valueOf(random.nextInt(899999)+100000);
//		List<Integer> postions = new ArrayList<>();
//		for(int i = 1;i<=gametea.getPersonnum();i++){
//			postions.add(i);
//		}
//		int num = 0;
//		if(strs!=null){
//			for (int i = 0; i < strs.length; i++) {
//				if(Integer.parseInt(strs[i]) == 6){
//					num++;
//				}
//			}
//			if(num==1){
//				room.setRoomCards(gameLogic.getCards(num));
//			}else{
//				room.setRoomCards(gameLogic.getCards(num));
//			}
//
//		}else{
//			room.setRoomCards(gameLogic.getCards(0));
//		}
//		room.setSpecialcard(sss);
//		room.setRoomName(roomName);
//		room.setUsers(new HashMap<String, UserBean>());
//		room.setLock(new ReentrantLock());
//		room.setTalkNum(0);
//		room.setIsUp(null);
//		room.setIsStart(0);
//		room.setGameId(0);
//		room.setRoomType(roomtype);
//		room.setPostions(postions);
//		room.setTimer(0);
//		rooms.put(roomName,room);
////		List<Map<String, Object>> positions = gameLogic.getAiPlayers(room,rooms);
//		System.out.println("gametea========"+gametea.getPersonnum());
//		roomTimer rt  = new roomTimer(room, Integer.valueOf(PropertiesUtil.getProperty("choose_time")),ws);
//		rt.start();
//		if(rooms.size()!=0){
//			return Body.BODY_200;
//		}
		return Body.BODY_400;

	}
	
	/*
	 *@Author:ZhaoQi
	 *@methodName:返回位置数组
	 *@Params:
	 *@Description:
	 *@Return:
	 *@Date:2019/2/27
	 */
	public static Integer[] getPostion(RoomsParam room,int num) {
		Map<String, UserBean> users = room.getUsers();
		Integer [] arr = new Integer[num];
		int t = 0;
		int i;
		for(i = 0 ;i < arr.length ; i++)
			arr[i] = 0;
		Iterator<Map.Entry<String, UserBean>> iterator = users.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String, UserBean> has = iterator.next();
			if(has.getValue().getType()!=0){
				if(has.getValue().getPosition()!=null){
					arr[has.getValue().getPosition()-1] = Integer.valueOf(String.valueOf(has.getValue().getId()));
				}
			}
		}
		return arr;
	}
	
	/**
	 * 删除元帅茶苑
	 * @param gametea
	 * @return
	 */
	public Body delroom(Gametea gametea) {
		Map<String, RoomsParam> rooms = null;
		for (RoomsParam room : rooms.values()) {
			if(room.getRoomName().equals(String.valueOf(gametea.getRoom()))){
				rooms.remove(String.valueOf(gametea.getRoom()));
				return Body.BODY_200;
			}
		}
		return Body.BODY_401;
	}

}
